#!/bin/bash

###
 # @Description: shell面试
 # @Version: 2.0
 # @Autor: gang
 # @Date: 2020-10-11 17:50:21
 # @LastEditors: gang
 # @LastEditTime: 2020-10-12 17:05:36
### 

###
 # @description: 批量生成随机文件名
 # @param {type} 
 # @return {type} 
 # @author: gang
###
function gener_file(){
    local baseDir=/home/test/randFile
    [ -d $baseDir ] || mkdir -p $baseDir  #如果定义的路径不存在，则创建
    
    for n in `seq 10`
    do
        filename=$(openssl rand -base64 40 | sed 's#[^a-z]##g' | cut -c 2-11)
        file=$baseDir/${filename}_multi.txt
        echo $file
        touch $file
    done
}

###
 # @description: 批量修改文件名
 # @param {type} 
 # @return {type} 
 # @author: gang
###
function mvFileName(){
    local baseDir=/home/test/randFile
    newFileName=_mvmulti.TXT
    for file in $(ls $baseDir)
    do
        name=$(echo $file | awk -F '_' '{print $1}') #定义替换后的目标字符串
        echo $baseDir/$name$newFileName
        if [ $baseDir/$file -eq $baseDir/$name$newFileName ]
            then
            continue
        else
            mv $baseDir/$file $baseDir/$name$newFileName
        fi
        
    done
}

###
 # @description: 批量创建Linux账号
 # @param {type} 
 # @return {type} 
 # @author: gang
#  useradd stu01 ;
#  pass=$((RANDOM+10000000));
#  echo "$pass"|passwd --stdin stu01;
#  echo -e "stu01        `echo "$pass"`">>/tmp/oldboy.log
###
function gener_user(){
    echo stu{01..10}|tr " " "\n"|sed -r 's#(.*)#useradd \1 ; pass=$((RANDOM+10000000)); echo "$pass"|passwd --stdin \1; echo -e "\1 \t `echo "$pass"`" >> /tmp/oldboy.log#g'|bash
    # echo stu{11..12}|xargs -n1 useradd ;echo stu{11..12}:`cat /dev/urandom|tr -dc 0-9|fold -w8|head -1`|xargs -n1|tee -a pass.txt|chpasswd
}


function main(){
    echo -----'批量生成随机文件名'
    # gener_file
    echo -----'批量修改文件名'
    # mvFileName
    # 批量创建用户
    gener_user
}

main


